/*
Note: This only works for state-by-year panel (not individual data)
*/

cap program drop createstack
program define createstack
args prominent smallcut smallcont rhs //prominent = size of increase smallcut = smaller mw to cut, smallcont = smaller mw to control

//Merge in federal mw 
preserve
use "${data2}mw_state_annual.dta" , clear
ren statefips state_fips
replace year = year+1
tempfile fedmw
save `fedmw'
restore

merge 1:1 state_fips year using `fedmw', keep(3) nogen keepusing(mean_fed_mw)
gen byte fed_mw = round(mean_fed_mw,.0001)==round(mw,.0001) 

sort state_fips year
xtset state_fips year

cap drop mw_change
gen mw_change = d1.mw
replace mw_change=0 if mw_change==.


gen tryear_eff = year if mw_change>=`prominent'/100

sort tryear_eff state_fips 
gen eventid=_n if tryear_eff!=.

qui sum eventid 
local max = r(max)


/*========= TAKE 1: =========*/

local eventno = ""
qui{
foreach i of numlist 1/`max' {
	preserve
	
	** Figure out Treatment State + Year
	sum tryear if eventid==`i'
	global tryear = r(mean)
				
	sum state_fips if eventid==`i'
	global trfips = r(mean)
	
	
	***Sample Cuts
				
	//Keep donor states w/o "treatment" 
	local lead = $lead
	local lag = $lag
	keep if inrange(year, $tryear -`lead', $tryear + `lag') 
	drop if mw_change >=`smallcut' & state_fips!=$trfips
				
	//Balanced Panel -- Treatment State should be balanced. 
	bys state_fips: gen count=_N
	qui sum count if state_fips==$trfips
	local numobs= r(mean)
	
	keep if count==`numobs'
	
	//Create smaller mw control
	gen small_inc = year if mw_change>=`smallcont'/100
	bys state_fips: egen small_year = min(small_inc) 
	gen byte mw_small = year>=small_year & state_fips!=$trfips
	
	//Keep only cases where there are control groups
	sum state_fips if state_fips!=$trfips
	local N =r(N) 
	if `N' >0  {
		sort state_fips year
		replace mw_change=0
		replace mw_change=1 if state_fips==$trfips & tryear==$tryear

		forval f = $lead(-1)1 {
			gen F`f'_D = F`f'.mw_change
			label var  F`f'_D "-`f'"
			replace F`f'_D = 0 if F`f'==.
			}
		gen treat = 0
		forval l = 0/$lag  {
			gen L`l'_D = L`l'.mw_change
			label var  L`l'_D "`l'"
			replace L`l'_D = 0 if L`l'==.
			replace treat = L`l'_D + treat
		}
		
		
		replace year = year -1
		merge m:1 state_fips year using "${data2}/aca_expansion_eitc.dta", keep(1 3) nogen
		replace year = year +1
		
		qui reg under_100 `rhs'
		predict y, resid 
		qui sdid y state_fips year  treat , vce(noinference) 
		tempfile temp
		qui save `temp', replace
		
		
		matrix weight = e(omega)
		svmat weight
		keep weight1 weight2
		ren (weight1 weight2) (omega state_fips )
		keep if state_fips!=.
		tempfile unitwt
		qui save `unitwt', replace
						
		matrix weight = e(lambda)
		svmat weight
		ren (weight1 weight2) (lambda year )
		keep if year!=.
		tempfile timewt
		save `timewt', replace
		
		use `temp', clear
		merge m:1 state_fips using `unitwt', nogen
		merge m:1 year using `timewt', nogen

		replace omega = 1 if state_fips==$trfips
		gen post = 1 if year>=$tryear
		bys state_fips: egen post2 = total(post)
		replace lambda = post/post2 if year>=$tryear
		drop post post2
		gen sdid_wt = omega*lambda
		
		gen event=`i'
		replace tryear=. if  state_fips!=$trfips
		tempfile event`i'
		save `event`i'', replace
		local eventno = "`eventno'" + "`i' " 
	}
	else {
	
	}
	restore
}

clear
foreach i of local eventno {
	append using `event`i''
}
}

replace year = year -1

end


cap program drop plotes
program define plotes

  local range yla(-0.04(0.01)0.04)
  		coefplot ///
  			(D, omitted keep(F*D L*D )  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' legend(off) nooffsets ///
  			ytitle("Estimated Effect of Prominent Minimum Wage") xtitle("Years Since Prominent Minimum Wage Increase")


end

global lead=3
global lag=3

***********************************************************8
local rhs ur_high_edu wage_high_edu hpi eitc tanf4 snap4 ACA mw_small
// Dube Period
foreach inc of numlist 25 50 75 100 {
	use ${data1}state_panel_mw_poverty_84, clear
	keep if year<2014

	if `inc'>50 {
		createstack `inc' 50 10 "`rhs'"
	} 
	else  {
	createstack `inc' 25 10 "`rhs'"
	}
	
	replace F1_D=0
	reghdfe under_100 F* L* `rhs' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdid_`inc'_8413, replace
	
	reghdfe under_100 treat `rhs' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdidatt_`inc'_8413, replace
}

//1984-2020
foreach inc of numlist 25 50 75 100 {
	use ${data1}state_panel_mw_poverty_84, clear

	if `inc'>50 {
		createstack `inc' 50 10 "`rhs'"
	} 
	else  {
	createstack `inc' 25 10 "`rhs'"
	}
	
	replace F1_D=0
	reghdfe under_100 F* L* `rhs' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdid_`inc'_84, replace
	
	reghdfe under_100 treat `rhs' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdidatt_`inc'_84, replace
}

***********************************************************8
local rhs2 gdppc ur eitc_dube 

// Dube Period: Dube Cont
use "${data1}regready_cps_ipums_84.dta", clear
bys state_fips year: keep if _n==1
keep state_fips year  gdppc ur eitc_dube 
tempfile dubecont
save `dubecont'

foreach inc of numlist 25 50 75 100 {
	use ${data1}state_panel_mw_poverty_84, clear
	merge 1:1 state_fips year using `dubecont'
	keep if year<2014

	if `inc'>50 {
		createstack `inc' 50 10 "`rhs2'"
	} 
	else  {
	createstack `inc' 25 10 "`rhs2'"
	}
	
	replace F1_D=0
	reghdfe under_100 F* L* `rhs2' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdid_`inc'_8413_dubecont, replace
	
	reghdfe under_100 treat `rhs2' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdidatt_`inc'_8413_dubecont, replace
}

//1984-2020: Dube Cont
use "${data1}regready_cps_ipums_84.dta", clear
bys state_fips year: keep if _n==1
keep state_fips year  gdppc ur eitc_dube 
tempfile dubecont
save `dubecont'
foreach inc of numlist 25 50 75 100 {
	use ${data1}state_panel_mw_poverty_84, clear
	merge 1:1 state_fips year using `dubecont'

	if `inc'>50 {
		createstack `inc' 50 10 "`rhs2'"
	} 
	else  {
	createstack `inc' 25 10 "`rhs2'"
	}
	
	replace F1_D=0
	reghdfe under_100 F* L* `rhs2' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdid_`inc'_84_dubecont, replace
	
	reghdfe under_100 treat `rhs2' [pw=sdid_wt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_sdidatt_`inc'_84_dubecont, replace
}



// Plot
foreach inc of numlist 25 50 75 100 {
  estimates use ${est}ster/prominent_stacked_sdid_`inc'_8413
  est store D
  plotes
  graph export "${fig}event_studies/stacked_sdid_8413_`inc'_all.png", replace
  
   estimates use ${est}ster/prominent_stacked_es_`inc'_84
  est store D
  plotes
  graph export "${fig}event_studies/stacked_sdid_84_`inc'_all.png", replace
}



*******************************************************8
// Create Tables: Full Period
outreg, clear
use ${data1}state_panel_mw_poverty_84, clear
cap gen treat = 1
qui sum under_100 if year<2014 [aw=asecwt]
local DVmean= r(mean)

est use ${est}ster/prominent_stacked_sdidatt_75_84_dubecont
lincom treat
local beta = `r(estimate)'
di `beta'
local elas:  di %5.3f `beta'/`DVmean'
di `elas'
outreg, store(s)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend keep(treat) ///
addrow("Elast", "[`elas']")

foreach inc of numlist 100 50 25 {
est use ${est}ster/prominent_stacked_sdidatt_`inc'_84_dubecont
lincom treat
  local beta = `r(estimate)'
  di `beta'
  local elas:  di %5.3f `beta'/`DVmean'
  di `elas'
  outreg, merge(s) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(treat) nolegend ///
  addrow("Elast", "[`elas']")
}

est use ${est}ster/prominent_stacked_sdidatt_75_84
lincom treat
local beta = `r(estimate)'
di `beta'
local elas:  di %5.3f `beta'/`DVmean'
di `elas'
outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend keep(treat) ///
addrow("Elast", "[`elas']")

foreach inc of numlist 100 50 25 {
est use ${est}ster/prominent_stacked_sdidatt_`inc'_84
lincom treat
  local beta = `r(estimate)'
  di `beta'
  local elas:  di %5.3f `beta'/`DVmean'
  di `elas'
  outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(treat) nolegend ///
  addrow("Elast", "[`elas']")
}


outreg, replay append(s)
outreg, replay append(r)

outreg, replay
outreg using "${est}stacked_sdid", statfont( fs12 ) basefont(fs12) replay replace landscape




*******************************************************8
// Create Tables: Dube Period
outreg, clear
use ${data1}state_panel_mw_poverty_84, clear
cap gen treat = 1
qui sum under_100 if year<2014 [aw=asecwt]
local DVmean= r(mean)

est use ${est}ster/prominent_stacked_sdidatt_75_8413_dubecont
lincom treat
local beta = `r(estimate)'
di `beta'
local elas:  di %5.3f `beta'/`DVmean'
di `elas'
outreg, store(s)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend keep(treat) ///
addrow("Elast", "[`elas']")

foreach inc of numlist 100 50 25 {
est use ${est}ster/prominent_stacked_sdidatt_`inc'_8413_dubecont
lincom treat
  local beta = `r(estimate)'
  di `beta'
  local elas:  di %5.3f `beta'/`DVmean'
  di `elas'
  outreg, merge(s) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(treat) nolegend ///
  addrow("Elast", "[`elas']")
}


est use ${est}ster/prominent_stacked_sdidatt_75_8413
lincom treat
local beta = `r(estimate)'
di `beta'
local elas:  di %5.3f `beta'/`DVmean'
di `elas'
outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend keep(treat) ///
addrow("Elast", "[`elas']")

foreach inc of numlist 100 50 25 {
est use ${est}ster/prominent_stacked_sdidatt_`inc'_8413
lincom treat
  local beta = `r(estimate)'
  di `beta'
  local elas:  di %5.3f `beta'/`DVmean'
  di `elas'
  outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(treat) nolegend ///
  addrow("Elast", "[`elas']")
}


outreg, replay append(s)
outreg, replay append(r)

outreg, replay
outreg using "${est}stacked_sdid_8413", statfont( fs12 ) basefont(fs12) replay replace landscape



